package cn.jiyun.controller;

import cn.jiyun.common.R;
import cn.jiyun.pojo.Cd;
import cn.jiyun.pojo.Dish;
import cn.jiyun.service.DishService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("dish")
@RestController
public class DishController {

    @Autowired
    private DishService  dishService;

    @GetMapping("page")
    public R page(Integer page, Integer pageSize, String  name){
        Page<Dish> page1=new Page<>(page,pageSize);
        return dishService.page(page1,name);
    }

//    回显
    @GetMapping("{id}")
    public R hx(@PathVariable Long id){
        Cd dish = dishService.hx(id);
        R r=new R();
        r.setCode(1);
        r.setData(dish);
        return r;
    }


    @GetMapping("list")
    public R list(Long categoryId){
      List<Dish> dish=  dishService.list(categoryId);
      return R.success(dish);
    }

    @PostMapping()
    public R add(@RequestBody Cd cd, HttpSession session){
        Long id= (Long) session.getAttribute("employee");
        return R.success(dishService.add(id,cd));
    }

    @PutMapping()
    public R update(@RequestBody Cd cd, HttpSession session){
//        Long id= (Long) session.getAttribute("employee");
        R r=new R();
        r.setCode(1);


        return R.success(dishService.update(cd,1l));
    }


    @DeleteMapping()
    public R del(Long[] ids, HttpSession session){
        Long idc= (Long) session.getAttribute("employee");
        for (Long id : ids) {
            dishService.del(id,idc);
        }
        return R.success("成功");
    }

    @PostMapping("/status/1")
    public R status1(Long [] ids){
        for (Long id : ids) {
            dishService.status1(id);
        }
        return R.success("成功");
    }

    @PostMapping("/status/0")
    public R status0(Long [] ids){
        for (Long id : ids) {
            dishService.status0(id);
        }
        return R.success("成功");
    }
}
